<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="展示屏名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入展示屏名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否展示" prop="online">
        <el-select v-model="queryParams.online" placeholder="请选择是否展示" clearable size="small">
          <el-option
            v-for="dict in onlineOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:display:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:display:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cms:display:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['cms:display:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="displayList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="展示屏名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="是否展示" align="center" prop="online" :formatter="onlineFormat" />
      <!-- <el-table-column label="描述" align="center" prop="discription" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleLogo(scope.row)"
            v-hasPermi="['cms:display:logo:list']"
            v-if="scope.row.type == 1"
          >关联LOGO</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleVideo(scope.row)"
            v-hasPermi="['cms:display:video:list']"
            v-if="scope.row.type > 1"
          >关联视频</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:display:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:display:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改展示屏管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="94px">
        <!-- <el-form-item label="背景图">
          <imageUpload v-model="form.background" :limit="1" />
        </el-form-item> -->
        <el-form-item label="展示屏名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入展示屏名称" />
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" type="input" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="是否展示">
          <el-radio-group v-model="form.online">
            <el-radio
              v-for="dict in onlineOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="discription">
          <el-input v-model="form.discription" type="textarea" rows="5" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- LOGO -->
    <el-dialog v-loading="loading" :title="displayTitle" :visible.sync="logoOpen" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddLogo" v-hasPermi="['cms:display:logo:list']">添加关联</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteLogo" v-hasPermi="['cms:display:logo:delete']">取消关联</el-button>
        </el-col>
      </el-row>

      <el-table :data="logoList" @selection-change="handleDisplayLogoSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="LOGO-ID" align="center" prop="id" width="100"/>
          <el-table-column label="LOGO名称" align="center" prop="name" />
          <el-table-column label="LOGO-ICON" align="center" prop="icon" >
            <el-image slot-scope="scope" :src="scope.row.icon" style="width: 100px; height: 100px" v-if="scope.row.icon"></el-image>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdateLogo(scope.row)"
                v-hasPermi="['cms:display:logo:edit']"
              >修改</el-button>
            
            </template>
          </el-table-column>
      </el-table>  
    </el-dialog>

    <!-- LOGO -->
    <el-dialog v-loading="loading" :title="logoTitle" :visible.sync="logoListOpen" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDisplayLogo" v-hasPermi="['cms:display:logo:add']">确定</el-button>
        </el-col>
      </el-row>

      <el-table :data="logos" @selection-change="handleLogoSelectionChange" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="LOGO-ID" align="center" prop="id" width="100"/>
          <el-table-column label="LOGO名称" align="center" prop="name" />
          <el-table-column label="LOGO-ICON" align="center" prop="icon" >
            <el-image slot-scope="scope" :src="scope.row.icon" style="width: 100px; height: 100px" v-if="scope.row.icon"></el-image>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" />
      </el-table>  
    </el-dialog>
    <!-- 修改关联logo -->
    <el-dialog :title="displayTitle" :visible.sync="editLogoOpen" width="600px" append-to-body>
      <el-form ref="form" :model="logoForm" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model="logoForm.sort" placeholder="请输入排序" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLogoForm">确 定</el-button>
        <el-button @click="cancelLogoForm">取 消</el-button>
      </div>
    </el-dialog>

    <!--  视频 -->
    <el-dialog v-loading="loading" :title="displayTitle" :visible.sync="videoOpen" width="600px" append-to-body>

      <el-table :data="videoList" >
          <el-table-column label="视频ID" align="center" prop="id" width="100"/>
          <el-table-column label="视频名称" align="center" prop="name" />
          <el-table-column label="视频" align="center" prop="video" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.displayId > 0"
                size="mini"
                 type="danger"
                icon="el-icon-delete"
                @click="handleDeleteVideo(scope.row)"
                v-hasPermi="['cms:display:video:delete']"
              >取消关联</el-button>
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-plus"
                @click="handleAddVideo(scope.row)"
                v-hasPermi="['cms:display:video:add']"
                v-else
              >添加关联</el-button>
            </template>
          </el-table-column>
      </el-table>  
    </el-dialog>

  </div>
</template>

<script>
import { listDisplay, getDisplay, delDisplay, addDisplay, updateDisplay, exportDisplay, listLogo, listLogos, addLogo, deleteLogo, editLogo, listVideo, deleteVideo, addVideo } from "@/api/cms/display";

export default {
  name: "Display",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 展示屏管理表格数据
      displayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      typeOptions: [],
      // 是否展示字典
      onlineOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        online: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "展示屏名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        online: [
          { required: true, message: "是否展示不能为空", trigger: "blur" }
        ],
      },
      // LOGO
      displayId: null,
      displayTitle: null,
      logoOpen: false,
      logoTitle: "Logo列表",
      logoList: [],
      logoListOpen: false,
      logos: [],
      displayLogo: [],
      logoIds: [],
      logoForm: {},
      editLogoOpen: false,
      //视频
      videoList: [],
      videoOpen: false,
    };
  },
  created() {
    this.getList();
    this.getDicts("display_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("online").then(response => {
      this.onlineOptions = response.data;
    });
  },
  methods: {
    /*********  关联视频 开始 ***********/
    handleAddVideo(row) {
      const data = {
        displayId: this.displayId,
        videoId: row.id
      }
      addVideo(data).then(response => {
        this.msgSuccess("添加成功");
        this.getVideoList();
      })
    },
    handleDeleteVideo(row) {
      const data = {
        displayId: this.displayId,
        videoId: row.id
      }
      deleteVideo(data).then(response => {
        this.msgSuccess("取消成功");
        this.getVideoList();
      })
    },
    handleVideo(row) {
      this.displayId = row.id;
      this.displayTitle = row.name + "—视频"
      this.videoOpen = true;
      this.getVideoList();
      console.log(this.videoList);
    },
    getVideoList() {
      this.loading = true;
      listVideo(this.displayId).then(response => {
        this.videoList = response.data;
        this.loading = false;
      })
    },
    /*********  关联视频 结束 ***********/
    /*********  关联logo 开始 ***********/
    cancelLogoForm() {
      this.editLogoOpen = false;
    },
    submitLogoForm() {
      editLogo(this.logoForm).then(response => {
        this.msgSuccess("修改成功");
        this.editLogoOpen = false;
        this.getLogoList();
      })
    },
    handleUpdateLogo(row) {
      this.logoForm = {
        displayId: this.displayId,
        logoId: row.id,
        sort: row.sort
      }
      this.editLogoOpen = true;
      
    },
    handleDeleteLogo() {
      deleteLogo(this.displayId, this.logoIds).then(response => {
        this.msgSuccess("取消成功");
        this.getLogoList();
      })
    },
    handleDisplayLogoSelectionChange(selection) {
      console.log(123123)
      this.logoIds = selection.map(item => item.id);
    },
    handleAddDisplayLogo() {
      console.log(this.displayLogo)
      addLogo(this.displayLogo).then(response => {
        this.msgSuccess("关联成功");
        this.logoListOpen = false;
        this.getLogoList();
      })
    },
    handleLogoSelectionChange(selection) {
      console.log(selection)
        selection.map(item => {
          item.displayId = this.displayId;
          item.logoId = item.id;
        })
        this.displayLogo = selection;
    },
    handleAddLogo() {
      this.logoListOpen = true;
      this.loading = true;
      listLogos(this.displayId).then(response => {
        this.logos = response.data;
        this.loading = false;
      })
    },
    handleLogo(row) {
      this.displayTitle = row.name + '—LOGO'
      this.displayId = row.id;
      this.logoOpen = true;
      this.getLogoList();
      
    },
    getLogoList() {
      this.loading = true;
      listLogo(this.displayId).then(response => {
        this.logoList = response.data;
        this.loading = false;
      })
    },
    /*********  关联logo 结束 ***********/
    /** 查询展示屏管理列表 */
    getList() {
      this.loading = true;
      listDisplay(this.queryParams).then(response => {
        this.displayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 是否展示字典翻译
    onlineFormat(row, column) {
      return this.selectDictLabel(this.onlineOptions, row.online);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        background: null,
        name: null,
        type: 1,
        sort: 1,
        online: 1,
        discription: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加展示屏管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDisplay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改展示屏管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDisplay(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDisplay(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除展示屏管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDisplay(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有展示屏管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDisplay(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
